package com.springbootee.Common.aop;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.springbootee.Common.base.UserHolder;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;


import java.util.Date;

@Component//容器

/**
 * @parms:
 * @return:
 * @author: dell
 * @time: 2021/10/12 19:23
 */
   public  class  MybatisMetaObjectHandler  implements MetaObjectHandler {
    //删除标识
    private static  final String DELETE_FALG="deleteFalg";
    //创建时间
    private static  final  String CREATE_TIME="createTime";
    //创建人ID
    private  static final  String  CREATE_USER_ID="createUserId";
    //更新时间
    private  static  final  String UPDATE_TIME="updateTime";
    //更新人ID
    private  static  final  String UPDATE_USER_ID="updateUserId";



    /**
     * @parms
     * @return: 自动填充字段
     * @author: dell
     * @time: 2021/10/12 19:25
     */
     @Override
     public void insertFill(MetaObject metaObject){
     this.setFieldValByName(DELETE_FALG,false,metaObject);
     this.setFieldValByName(CREATE_TIME,new Date(),metaObject);
     this.setFieldValByName(CREATE_USER_ID, UserHolder.getUser().getUserId(),metaObject);
     this.setFieldValByName(UPDATE_TIME,new Date(),metaObject);
     this.setFieldValByName(UPDATE_USER_ID,UserHolder.getUser().getUserId(),metaObject);
     }



     /**
      * @parms
      * @return: 更新填充字段
      * @author: dell
      * @time: 2021/10/12 19:25
      */
      @Override
     public void updateFill(MetaObject metaObject){
        this.setFieldValByName(UPDATE_TIME, new Date(),metaObject);
        this.setFieldValByName(UPDATE_USER_ID,UserHolder.getUser().getUserId(),metaObject);
    }
}
